package untitled1;
import java.util.*;

// 先创建一个学生类
class Student{
    private int id;  // 学生的学号
    private int grade;  // 学生的成绩
    // 构造方法
    public Student(int id, int grade) {
        this.id = id;
        this.grade = grade;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getGrade() {
        return grade;
    }

    public void setGrade(int grade) {
        this.grade = grade;
    }
}


public class Text2 {
    public static void main(String[] args) {
        // 集合存放40个学生的信息
        ArrayList<Student> list = new ArrayList<Student>();
        // 生成随机数
        Random random = new Random();
        for (int i = 0; i < 40; i++) {
            int id = 2020042901+i;
            int grade = random.nextInt(41)+60; // [0,41)+60--->[60,101)==[60,100]
            list.add(new Student(id,grade));
        }
        // 排序，按成绩由高到低排，成绩相同的按学号大小先排
        Comparator<Student> ct = new Comparator<Student>(){
            @Override
            public int compare(Student o1, Student o2) {
                return o2.getGrade()>o1.getGrade()?1:o2.getGrade()<o1.getGrade()?-1:o1.getGrade()-o2.getGrade();
            }
        };
        Collections.sort(list,ct);
        // 迭代器进行输出
        Iterator<Student> iterator = list.iterator();
        while (iterator.hasNext()){
            Student  stu = iterator.next();
            System.out.println("学号："+stu.getId()+"：Java集合阶段检测成绩"+stu.getGrade());
        }
    }
}
